// 微信底部弹出
import 'package:flutter/material.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';
import 'package:weichat_post/utils/index.dart';
import 'package:weichat_post/utils/picker.dart';
import 'package:weichat_post/widgets/index.dart';

enum PickType { camera, asset }

// 微信底部弹出
class DuBottomSheet {
  DuBottomSheet({List<AssetEntity>? selectedAssets})
      : selectedAssets = selectedAssets ?? [];

  /// 已经选中的资源
  final List<AssetEntity> selectedAssets;

  /// 选择拍摄、洗相机资源
  Future<T?> wxPicker<T>(BuildContext context) {
    return DuPicker.showModalSheet<T>(
      context,
      child: _buildAssetCamera(context),
    );
  }

// 拍摄、相册
  Column _buildAssetCamera(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        // 拍摄
        _buildBtn(
          const Column(
            children: [
              Text(
                "拍摄",
                style: TextStyle(fontSize: 18, color: Colors.black87),
              ),
              Text(
                "照片或视频",
                style: TextStyle(fontSize: 12, color: Colors.black45),
              ),
            ],
          ),
          onTap: () async {
            final AssetEntity? result = await CameraPicker.pickFromCamera(
              context,
              pickerConfig: const CameraPickerConfig(enableRecording: true),
            );

            if (result == null) return;

            print("拍摄$result");

            if (result.type == AssetType.image) {
              selectedAssets.add(result);
            }

            if (result.type == AssetType.video) {
              selectedAssets.clear();
              selectedAssets.add(result);
            }
            Navigator.pop(context, selectedAssets);
          },
        ),
        const DividerWidget(),
        // 相册
        _buildBtn(
          const Text(
            "从相册中选择",
            style: TextStyle(fontSize: 18, color: Colors.black87),
          ),
          onTap: () async {
            DuPicker.showModalSheet(
              context,
              child: _buildMediaImageVideo(context),
            );
          },
        ),
        const DividerWidget(
          height: 10,
        ),
        // 取消
        _buildBtn(
          const Text(
            "取消",
            style: TextStyle(fontSize: 18, color: Colors.black87),
          ),
          onTap: () {
            Navigator.pop(context);
          },
        ),
      ],
    );
  }

  // 照片、视频
  Widget _buildMediaImageVideo(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        _buildBtn(
          const Text(
            "照片",
            style: TextStyle(fontSize: 18, color: Colors.black87),
          ),
          onTap: () async {
            print("selectedAssets.length ${selectedAssets.length}");
            final List<AssetEntity>? result = await AssetPicker.pickAssets(
              context,
              pickerConfig: AssetPickerConfig(
                selectedAssets: selectedAssets,
                requestType: RequestType.image,
                maxAssets: maxAssets,
              ),
            );
            if (result == null || result.isEmpty) {
              return;
            }
            selectedAssets.addAll(result);
            _popRoute(context, result: result);
          },
        ),
        const DividerWidget(),
        _buildBtn(
          const Text(
            "视频",
            style: TextStyle(fontSize: 18, color: Colors.black87),
          ),
          onTap: () async {
            final List<AssetEntity>? result = await AssetPicker.pickAssets(
              context,
              pickerConfig: AssetPickerConfig(
                selectedAssets: selectedAssets,
                requestType: RequestType.video,
                maxAssets: 1,
              ),
            );
            if (result == null || result.isEmpty) {
              return;
            }
            selectedAssets.clear();
            selectedAssets.addAll(result);
            _popRoute(context, result: selectedAssets);
          },
        ),
        const DividerWidget(
          height: 10,
        ),
        _buildBtn(
          const Text(
            "取消",
            style: TextStyle(fontSize: 18, color: Colors.black87),
          ),
          onTap: () {
            _popRoute(context);
          },
        )
      ],
    );
  }

// 返回
  void _popRoute(BuildContext context, {result}) {
    Navigator.pop(context);
    Navigator.pop(context, result);
  }

  // 按钮
  static InkWell _buildBtn(Widget child, {Function()? onTap}) {
    return InkWell(
      onTap: onTap,
      child: Container(
        alignment: Alignment.center,
        height: 50,
        child: child,
      ),
    );
  }
}
